<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Polls
 * @description : voting system
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Polls extends AUTH_Controller {

  /**
   * Polls list
   * 
   */
  public function index($polls_limit = 10) {

    // Init vars
    $polls_type = NULL;
    $polls_data = NULL;
    $polls_list_type = $this->input->get('list_type', TRUE);

    // Get polls according to type
    if ($polls_list_type == 'top') {
      $get_polls = $this->poll_model->get_top_polls($polls_limit, $this->current_user['user_id']);
      $get_polls_count = $this->poll_model->get_top_polls(NULL, $this->current_user['user_id']);
    } elseif ($polls_list_type == 'my') {
      $get_polls = $this->poll_model->get_my_polls($polls_limit, $this->current_user['user_id']);
      $get_polls_count = $this->poll_model->get_my_polls(NULL, $this->current_user['user_id']);
    } elseif ($polls_list_type == 'new') {
      $get_polls = $this->poll_model->get_polls($polls_limit, $this->current_user['user_id']);
      $get_polls_count = $this->poll_model->get_polls(NULL, $this->current_user['user_id']);
    } elseif ($polls_list_type == 'completed') {
      $get_polls = $this->poll_model->get_comp_polls($polls_limit, $this->current_user['user_id']);
      $get_polls_count = $this->poll_model->get_comp_polls(NULL, $this->current_user['user_id']);
    } else {
      $get_polls = $this->poll_model->get_active_polls($polls_limit, $this->current_user['user_id']);
      $get_polls_count = $this->poll_model->get_active_polls(NULL, $this->current_user['user_id']);
    }

    if ($get_polls['status'] == 1 && !empty($get_polls['result'])) {
      $polls_data = $get_polls['result'];
    }

    // Addtional polls details
    $get_new_polls_count = $this->poll_model->get_new_polls_count();
    $new_polls_count = $get_new_polls_count['result'];

    // Preparing & loading view
    $view_data = array();
    $view_data['menu_active'] = 'polls';
    $view_data['polls_type'] = $polls_type;
    $view_data['polls_data'] = $polls_data;
    $view_data['polls_count'] = $get_polls_count['count'];
    $view_data['polls_limit'] = $polls_limit;
    $view_data['new_polls_count'] = $new_polls_count;
    $view_data['polls_list_type'] = $polls_list_type;
    $this->load_view('polls', $view_data);
    return;
  }

  /**
   * Create new poll
   * 
   */
  public function create() {

    // Init poll creation status
    $new_poll_status = NULL;

    // Checking request type
    if ($this->is_post()) {

      $new_poll_status = 2;

      // Getting & cleaning poll inputs
      $poll_message = $this->sanitize($this->input->post('poll_message', TRUE));
      $poll_options = @array_filter($this->input->post('poll_options', TRUE));
      $poll_show_users = $this->input->post('poll_show_users', TRUE);
      $poll_show_users = ($poll_show_users == 1) ? $poll_show_users : 2;
      $poll_add_option = $this->input->post('poll_add_option', TRUE);
      $poll_add_option = ($poll_add_option == 1) ? $poll_add_option : 2;

      // Checking inputs
      if (!empty($poll_message) && !empty($poll_options) && is_array($poll_options) && count($poll_options) >= 2) {

        // Insert new poll
        $set_poll = $this->poll_model->set_poll($poll_message, $this->current_user['user_id'], $poll_show_users, $poll_add_option);

        // Check insertion
        if ($set_poll['status'] == 1 && !empty($set_poll['result'])) {

          $poll_id = $set_poll['result']['poll_id'];

          // Cleaning & inserting poll option
          foreach ($poll_options as $poll_option) {
            $poll_option = $this->sanitize($poll_option);
            $poll_option = (!empty($poll_option)) ? $poll_option : '[removed]';
            $this->poll_model->set_poll_option($poll_id, $poll_option, $this->current_user['user_id']);
          }

          // Setting poll creation status
          $new_poll_status = 1;
          $this->session->set_flashdata('new_poll_status', $new_poll_status);
          $this->session->set_flashdata('poll_poll_id', $poll_id);
          $this->session->set_flashdata('poll_poll_hash', md5($poll_message));

          // Redirect to new poll page
          redirect('polls/view/' . $poll_id);
          return;
        }
      }

      // Setting error status 
      $this->session->set_flashdata('new_poll_status', $new_poll_status);
      redirect('polls/create');
      return;
    }

    // Preparing & loading view
    $view_data = array();
    $view_data['menu_active'] = 'polls';
    $this->load_view('polls_create', $view_data);
    return;
  }

  /**
   * Show normal polls
   * 
   */
  public function view($poll_id = NULL) {

    // Check poll id
    if (!empty($poll_id) && is_numeric($poll_id)) {

      // Get poll data
      $get_poll = $this->poll_model->get_poll_by_id($poll_id, NULL);

      // Check poll exists
      if ($get_poll['status'] == 1 && !empty($get_poll['result'])) {

        // If poll is completed, redirect to poll completed page
        if ($get_poll['result']['poll_status'] == 2) {
          redirect('polls/view_completed/' . $poll_id);
        }

        // Get poll options
        $get_poll_options = $this->poll_model->get_poll_options($poll_id);

        // Check poll options exists
        if ($get_poll_options['status'] == 1 && !empty($get_poll_options['result'])) {

          // Injecting poll opions to poll data
          $get_poll['result']['poll_options'] = $get_poll_options['result'];
          $poll_data = $get_poll['result'];
          $poll_reply_status = FALSE;
          $poll_reply_option = FALSE;
          $poll_reply_count = 0;

          // Setting current users reply
          $get_poll_reply = $this->poll_model->get_poll_reply($poll_id, $this->current_user['user_id']);
          if ($get_poll_reply['status'] == 1 && !empty($get_poll_reply['result'])) {
            $poll_reply_status = TRUE;
            $poll_reply_option = $get_poll_reply['result']['poll_reply_option_id'];
          }

          // Getting poll addional details
          $get_poll_reply_count = $this->poll_model->get_poll_reply_count($poll_id);
          if ($get_poll_reply_count['status'] == 1 && !empty($get_poll_reply_count['result'])) {
            $poll_reply_count = $get_poll_reply_count['count'];
          }

          // Getting poll comments
          $comment_type = 'poll';
          $comment_type_id = $poll_id;
          $comments_data = array();
          $total_comments_count = 0;
          $comments_limit = $this->input->get('limit', TRUE);
          $comments_limit = (is_numeric($comments_limit)) ? $comments_limit : 10;
          $get_comments = $this->comment_model->get_comments($comment_type, $comment_type_id, $comments_limit);
          $get_comments_count = $this->comment_model->get_comments($comment_type, $comment_type_id, NULL);
          if ($get_comments['status'] == 1 && !empty($get_comments['result'])) {
            $comments_data = $get_comments['result'];
            $total_comments_count = $get_comments_count['count'];
          }

          // Preparing view
          $view_data = array();
          $view_data['menu_active'] = 'polls';
          $view_data['poll_data'] = $poll_data;
          $view_data['poll_reply_status'] = $poll_reply_status;
          $view_data['poll_reply_option'] = $poll_reply_option;
          $view_data['poll_reply_count'] = $poll_reply_count;
          $view_data['comment_type'] = $comment_type;
          $view_data['comment_type_id'] = $comment_type_id;
          $view_data['comments_data'] = $comments_data;
          $view_data['total_comments_count'] = $total_comments_count;
          $view_data['comments_limit'] = $comments_limit;

          $this->load_view('polls_view', $view_data);
          return;
        }
      }
    }

    // Redirect if someting wrong
    redirect('polls');
  }

  /**
   * Show completed polls
   * 
   */
  public function view_completed($poll_id = NULL) {

    // Check poll id
    if (!empty($poll_id) && is_numeric($poll_id)) {

      // Get poll data
      $get_poll = $this->poll_model->get_poll_by_id($poll_id, 2);

      // Check poll
      if ($get_poll['status'] == 1 && !empty($get_poll['result'])) {

        // Get poll options
        $get_poll_options = $this->poll_model->get_poll_options($poll_id);

        // Check poll options
        if ($get_poll_options['status'] == 1 && !empty($get_poll_options['result'])) {

          // Inject poll optons to poll data
          $get_poll['result']['poll_options'] = $get_poll_options['result'];
          $poll_data = $get_poll['result'];
          $poll_reply_status = FALSE;
          $poll_reply_option = FALSE;
          $poll_reply_count = 0;

          // Setting current users reply
          $get_poll_reply = $this->poll_model->get_poll_reply($poll_id, $this->current_user['user_id']);
          if ($get_poll_reply['status'] == 1 && !empty($get_poll_reply['result'])) {
            $poll_reply_status = TRUE;
            $poll_reply_option = $get_poll_reply['result']['poll_reply_option_id'];
          }

          // Getting additional poll details
          $get_poll_reply_count = $this->poll_model->get_poll_reply_count($poll_id);
          if ($get_poll_reply_count['status'] == 1 && !empty($get_poll_reply_count['result'])) {
            $poll_reply_count = $get_poll_reply_count['count'];
          }

          // Getting poll comments
          $comment_type = 'poll';
          $comment_type_id = $poll_id;
          $comments_data = array();
          $total_comments_count = 0;
          $comments_limit = $this->input->get('comments_limit', TRUE);
          $comments_limit = (is_numeric($comments_limit)) ? $comments_limit : 10;
          $get_comments = $this->comment_model->get_comments($comment_type, $comment_type_id, $comments_limit);
          $get_comments_count = $this->comment_model->get_comments($comment_type, $comment_type_id, NULL);
          if ($get_comments['status'] == 1 && !empty($get_comments['result'])) {
            $comments_data = $get_comments['result'];
            $total_comments_count = $get_comments_count['count'];
          }

          // Preparing view
          $view_data = array();
          $view_data['menu_active'] = 'polls';
          $view_data['poll_data'] = $poll_data;
          $view_data['poll_reply_status'] = $poll_reply_status;
          $view_data['poll_reply_option'] = $poll_reply_option;
          $view_data['poll_reply_count'] = $poll_reply_count;
          $view_data['comment_type'] = $comment_type;
          $view_data['comment_type_id'] = $comment_type_id;
          $view_data['comments_data'] = $comments_data;
          $view_data['total_comments_count'] = $total_comments_count;
          $view_data['comments_limit'] = $comments_limit;

          $this->load_view('polls_view', $view_data);
          return;
        }
      }
    }

    // Redirect if somethig wrong
    redirect('polls');
  }

  public function reply($poll_id = NULL) {
    $new_poll_status = 2;
    $poll_selected_option = $this->input->post('poll_option_reply', TRUE);
    $add_option_msg = $this->sanitize($this->input->post('new_poll_option_msg', TRUE));
    if (!empty($poll_id) && is_numeric($poll_id) && !empty($poll_selected_option)) {
      $get_poll = $this->poll_model->get_poll_by_id($poll_id);
      if ($get_poll['status'] == 1 && !empty($get_poll['result'])) {
        $get_poll_options = $this->poll_model->get_poll_options($poll_id);
        if ($get_poll_options['status'] == 1 && !empty($get_poll_options['result'])) {
          $get_poll_reply = $this->poll_model->get_poll_reply($poll_id, $this->current_user['user_id']);
          if ($get_poll_reply['status'] == 1 && empty($get_poll_reply['result'])) {
            $poll_option_ids = array();
            foreach ($get_poll_options['result'] as $optns) {
              $poll_option_ids[] = $optns['poll_option_id'];
            }
            if (is_numeric($poll_selected_option) && in_array($poll_selected_option, $poll_option_ids)) {
              $this->poll_model->set_poll_reply($poll_id, $poll_selected_option, $this->current_user['user_id']);
              $new_poll_status = 3;
            }
            if ($poll_selected_option == 'add' && !empty($add_option_msg) && ($get_poll['result']['poll_add_option'] == 1)) {
              $set_poll_option = $this->poll_model->set_poll_option($poll_id, $add_option_msg, $this->current_user['user_id']);
              if ($set_poll_option['status'] == 1 && !empty($set_poll_option['result'])) {
                $new_poll_option_id = $set_poll_option['result'];
                $this->poll_model->set_poll_reply($poll_id, $new_poll_option_id, $this->current_user['user_id']);
                $new_poll_status = 3;
              }
            }
          }
        }
      }
    }
    $this->session->set_flashdata('new_poll_status', $new_poll_status);
    redirect('polls/view/' . $poll_id);
  }

  public function show_users() {
    if ($this->is_post() && $this->is_ajax()) {
      $poll_id = $this->input->post('poll_id', TRUE);
      $poll_option_id = $this->input->post('poll_option_id', TRUE);
      if (!empty($poll_id) && is_numeric($poll_id) && !empty($poll_option_id) && is_numeric($poll_option_id)) {
        $get_poll = $this->poll_model->get_poll_by_id($poll_id);
        if (empty($get_poll['result'])) {
          $get_poll = $this->poll_model->get_poll_by_id($poll_id, 2);
        }
        if ($get_poll['status'] == 1 && !empty($get_poll['result'])) {
          if ($get_poll['result']['poll_show_users'] == 1) {
            $get_poll_reply_users = $this->poll_model->get_poll_reply_users($poll_id, $poll_option_id);
            if ($get_poll_reply_users['status'] == 1 && !empty($get_poll_reply_users['result'])) {
              $view_data = array();
              $view_data['poll_data'] = $get_poll['result'];
              $view_data['poll_reply_data'] = $get_poll_reply_users['result'];
              $this->load->view('polls_show_users', $view_data);
            }
          }
        }
      }
    }
    return;
  }

  public function open($poll_id = NULL) {
    $poll_op_status = 'error';
    if (!empty($poll_id) && is_numeric($poll_id)) {
      if ($this->is_admin) {
        $update_poll_status_by_id = $this->poll_model->update_poll_status_by_admin($poll_id, 1);
        if ($update_poll_status_by_id['status'] == 1 && !empty($update_poll_status_by_id['result'])) {
          $poll_op_status = 1;
        }
      } else {
        $update_poll_status_by_id = $this->poll_model->update_poll_status_by_id($poll_id, $this->current_user['user_id'], 1);
        if ($update_poll_status_by_id['status'] == 1 && !empty($update_poll_status_by_id['result'])) {
          $poll_op_status = 1;
        }
      }
    }
    $this->session->set_flashdata('poll_op_status', $poll_op_status);
    redirect('polls?list_type=active');
    return;
  }

  public function close($poll_id = NULL) {
    $poll_op_status = 'error';
    if (!empty($poll_id) && is_numeric($poll_id)) {
      if ($this->is_admin) {
        $update_poll_status_by_id = $this->poll_model->update_poll_status_by_admin($poll_id, 2);
        if ($update_poll_status_by_id['status'] == 1 && !empty($update_poll_status_by_id['result'])) {
          $poll_op_status = 2;
        }
      } else {
        $update_poll_status_by_id = $this->poll_model->update_poll_status_by_id($poll_id, $this->current_user['user_id'], 2);
        if ($update_poll_status_by_id['status'] == 1 && !empty($update_poll_status_by_id['result'])) {
          $poll_op_status = 2;
        }
      }
    }
    $this->session->set_flashdata('poll_op_status', $poll_op_status);
    redirect('polls?list_type=completed');
    return;
  }

  public function delete($poll_id = NULL) {
    $poll_op_status = 'error';
    if (!empty($poll_id) && is_numeric($poll_id)) {
      if ($this->is_admin) {
        $update_poll_status_by_id = $this->poll_model->update_poll_status_by_admin($poll_id, 0);
        if ($update_poll_status_by_id['status'] == 1 && !empty($update_poll_status_by_id['result'])) {
          $this->like_model->del_likes_by_post('poll', $poll_id);
          $this->comment_model->del_comment_by_post('poll', $poll_id);
          $poll_op_status = 3;
        }
      } else {
        $update_poll_status_by_id = $this->poll_model->update_poll_status_by_id($poll_id, $this->current_user['user_id'], 0);
        if ($update_poll_status_by_id['status'] == 1 && !empty($update_poll_status_by_id['result'])) {
          $this->like_model->del_likes_by_post('poll', $poll_id);
          $this->comment_model->del_comment_by_post('poll', $poll_id);
          $poll_op_status = 3;
        }
      }
    }
    $this->session->set_flashdata('poll_op_status', $poll_op_status);
    redirect('polls');
    return;
  }

  /**
   * Load new poll (Ajax)
   * 
   */
  public function load_new_reply_and_option() {

    // Check request type
    if ($this->is_post() && $this->is_ajax()) {

      $response = array();
      $response['total_replies_count'] = 0;
      $response['poll_option_new_last_id'] = 0;
      $response['new_options_html'] = NULL;
      $response['options_updates'] = array('option_reply_count' => NULL, 'option_reply_percentage' => NULL);

      $poll_id = $this->input->post('poll_id', TRUE);
      $poll_option_last_id = $this->input->post('poll_option_last_id', TRUE);

      if (!empty($poll_id) && is_numeric($poll_id) && !empty($poll_option_last_id) && is_numeric($poll_option_last_id)) {

        $get_poll = $this->poll_model->get_poll_by_id($poll_id);

        if ($get_poll['status'] == 1 && !empty($get_poll['result'])) {

          $poll_reply_status = FALSE;
          $get_poll_reply = $this->poll_model->get_poll_reply($poll_id, $this->current_user['user_id']);
          if ($get_poll_reply['status'] == 1 && !empty($get_poll_reply['result'])) {
            $poll_reply_status = TRUE;
          }

          $view_data = array();
          $view_data['poll_data'] = $get_poll['result'];
          $view_data['poll_reply_status'] = $poll_reply_status;

          // Total replies count
          $poll_reply_count = 0;
          $get_poll_reply_count = $this->poll_model->get_poll_reply_count($poll_id);
          if ($get_poll_reply_count['status'] == 1 && !empty($get_poll_reply_count['result'])) {
            $poll_reply_count = $get_poll_reply_count['count'];
          }

          // Getting new options
          $new_options_html = NULL;
          $poll_option_new_last_id = 0;
          $get_new_options = $this->poll_model->get_new_options($poll_id, $poll_option_last_id);
          if (!empty($get_new_options['result'])) {
            foreach ($get_new_options['result'] as $poll_option_new) {

              $poll_option_reply_count = get_poll_reply_count($poll_option_new['poll_option_id']);
              $poll_option_percent = @ceil((($poll_option_reply_count / $poll_reply_count) * 100));

              $view_data['poll_option'] = $poll_option_new;
              $view_data['poll_option_reply_count'] = $poll_option_reply_count;
              $view_data['poll_option_percent'] = $poll_option_percent;
              $html_data = array();
              $view_data['type'] = 1;
              $html_data['reply_html_1'] = trim($this->load->view('polls_option_new', $view_data, TRUE));
              $view_data['type'] = 2;
              $html_data['reply_html_2'] = trim($this->load->view('polls_option_new', $view_data, TRUE));
              $html_data['poll_option_id'] = $poll_option_new['poll_option_id'];
              $new_options_html[] = $html_data;
            }
            $poll_option_new_last_id = $get_new_options['result'][0]['poll_option_id'];
          }

          $poll_option_updates = NULL;
          $get_poll_options = $this->poll_model->get_poll_options($poll_id);
          if ($get_poll_options['status'] == 1 && !empty($get_poll_options['result'])) {
            foreach ($get_poll_options['result'] as $poll_option) {
              $poll_option_reply_count = get_poll_reply_count($poll_option['poll_option_id']);
              $poll_option_percent = @ceil((($poll_option_reply_count / $poll_reply_count) * 100));
              $poll_option_updates[] = array('poll_option_id' => $poll_option['poll_option_id'], 'poll_option_reply_count' => $poll_option_reply_count, 'poll_option_percentage' => $poll_option_percent);
            }
          }

          $response['total_replies_count'] = $poll_reply_count;
          $response['poll_option_new_last_id'] = $poll_option_new_last_id;
          $response['new_options_html'] = $new_options_html;
          $response['options_updates'] = $poll_option_updates;
        }
      }

      echo json_encode($response);
      return;
    }
    redirect();
  }

}

?>
